/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ZoomDialog extends JDialog {
	private JSpinner spinner;
	private GridBagConstraints constraints = new GridBagConstraints();
	private Insets insets = new Insets(5, 5, 5, 5);
	private BoardWidget boardWidget;
	private GUI gui;
	
	private ChangeListener changeListener = new ChangeListener() {
		@Override
		public void stateChanged(ChangeEvent e) {
			int zoom = (Integer) ((JSpinner) e.getSource()).getModel().getValue();
			getBoardWidget().setZoom(zoom);
			gui.refresh();
		}
	};
	
	private ZoomListener zoomListener = new ZoomListener() {
		@Override
		public void zoomChanged(ZoomEvent zoomEvent) {
			spinner.setValue(getBoardWidget().getZoom());
		}
	};
	
	private void initialize(GUI gui) {
		this.gui = gui;
		this.spinner = new JSpinner(new SpinnerNumberModel(50, 25, 360, 1));
		this.spinner.addChangeListener(this.changeListener);
		this.setTitle("Zoom");
		this.getContentPane().setLayout(new GridBagLayout());
		this.constraints.insets = this.insets;
		this.getContentPane().add(new JLabel("Set Zoom "), constraints);
		this.getContentPane().add(spinner, constraints);
		
		this.setMinimumSize(new Dimension(125, 20));
		this.pack();
		this.setResizable(false);
	}
	
	public ZoomDialog(GUI gamePlayer) {
		super(gamePlayer, false);
		
		this.initialize(gamePlayer);
	}
	
	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		this.gui.refresh();
	}
	
	private BoardWidget getBoardWidget() {
		return this.boardWidget;
	}
	
	public void setBoardWidget(BoardWidget boardWidget) {
		this.boardWidget = boardWidget;
		this.getBoardWidget().addZoomListener(this.zoomListener);
		this.spinner.setValue(this.getBoardWidget().getZoom());
	}
}
